#include "operate_login_information.h"

#include "global_variant.h"

#include<QRegExp>
#include<QRegExpValidator>
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QDebug>
#include <QCryptographicHash>
#include <QSqlError>

OperateLoginInformation::OperateLoginInformation()
{

}

int OperateLoginInformation::getdatabaseOperateResult()
{
    return m_databaseOperateResult;
}

int OperateLoginInformation::judegeAccount(QString account, QString password)
{
    //处理密码，把密码转为Sha256
    QCryptographicHash passwordHash(QCryptographicHash::Sha256);
    QByteArray tmpPassword(password.toUtf8());
    passwordHash.addData(tmpPassword);
    password = passwordHash.result().toHex();

    QSqlQuery query(QSqlDatabase::database("mysql_connection1"));
    QString tmpSql = QString("SELECT CASE WHEN password = \'%1\' THEN 1 ELSE 0 END FROM login_information WHERE account = \'%2\'").arg(password).arg(account);
    //查询账号是否存在
    qDebug()<<"int OperateLoginInformation::judegeAccount:"<<tmpSql;
    if(query.exec(tmpSql)){
        while(query.next()){
            QVariant value = query.value(0);
            if(value.isNull()){
                m_databaseOperateResult = DatabaseOperateStatus::AccountNoExistentType;
                //qDebug()<<"登录判断：账号不存在";
            }else if(value.toInt() == 0){
                m_databaseOperateResult = DatabaseOperateStatus::PasswordWrongType;
                //qDebug()<<"登录判断：密码错误";
            }else if(value.toInt() == 1){
                m_databaseOperateResult = DatabaseOperateStatus::PasswordRightType;
                //qDebug()<<"登录判断：密码正确";
            }
        }
    }else{
        qDebug()<<query.lastError().databaseText();
        m_databaseOperateResult = DatabaseOperateStatus::DatabaseSqlOperateWrong;
    }


    return m_databaseOperateResult;
}

int OperateLoginInformation::registerAccount(QString account, QString password)
{
    QSqlQuery query(QSqlDatabase::database("mysql_connection1"));

    query.exec(QString("SELECT COUNT(account) FROM login_information WHERE account = \'%1\'").arg(account));
    //qDebug()<<"sql:"<<QString("SELECT COUNT(account) FROM login_information WHERE account = \'%1\'").arg(account);
    while(query.next()){
        qDebug()<<"query.value(0).toString():"<<query.value(0).toString();
        if(query.value(0).toInt() != 0){
            m_databaseOperateResult = DatabaseOperateStatus::AccountAlreadyExistentType;
            return m_databaseOperateResult;
        }
    }

    //转化密码数据
    QCryptographicHash passwordHash(QCryptographicHash::Sha256);
    QByteArray passwordSha256;
    passwordSha256.append(password.toUtf8());
    passwordHash.addData(passwordSha256);
    password = passwordHash.result().toHex();

    if(query.exec(QString("INSERT INTO login_information(account, password) VALUES (\'%1\', \'%2\')").arg(account).arg(password))){
        m_databaseOperateResult =  DatabaseOperateStatus::RegisterSuccessType;
    }else{
        m_databaseOperateResult = DatabaseOperateStatus::RegisterFailType;
        qDebug()<<query.lastError().databaseText();
    }
    return m_databaseOperateResult;
}

int OperateLoginInformation::alterAccount(QString previousAccount, QString nowAccount, QString password)
{
    //修改过密码后，需要重新把密码转换为sha256

    QCryptographicHash passwordHash(QCryptographicHash::Sha256);
    QByteArray passwordSha256;
    passwordSha256.append(password.toUtf8());
    passwordHash.addData(passwordSha256);
    password = passwordHash.result().toHex();

    QSqlDatabase db = QSqlDatabase::database("mysql_connection1");
    QSqlQuery query(db);
    QString tmpSql = QString("UPDATE login_information SET password = \'%1\', account = \'%2\'  WHERE account = \'%3\'").arg(password).arg(nowAccount).arg(previousAccount);
    //qDebug()<<"StudentInformationDialog::updateLoginInformation2Database():"<<tmpSql;
    if(!query.exec(tmpSql)){//无论是否更新，结果都会返回true
        qDebug()<<query.lastError().databaseText();

        m_databaseOperateResult = DatabaseOperateStatus::AlterRegisterFailType;
    }else{
        if(query.numRowsAffected() > 0){//判断实际生效行数是否大于0
            m_databaseOperateResult = DatabaseOperateStatus::AlterRegisterSuccessType;
        }else{
            m_databaseOperateResult = DatabaseOperateStatus::AlterRegisterNoAffectedType;
        }
    }
    return m_databaseOperateResult;
}

int OperateLoginInformation::getAccountPermission(QString account)
{
    QSqlDatabase db = QSqlDatabase::database("mysql_connection1");
    QSqlQuery query(db);
    QString tmpSql = QString("SELECT role_id FROM permission_manage_information WHERE user_id = \'%1\'").arg(account);
    if(query.exec(tmpSql)){
        while(query.next()){
            QString data = query.value(0).toString();
            if(data == "0"){
                return DatabaseOperatePermission::Administrator;
            }else if(data == "1"){
                return DatabaseOperatePermission::Teacher;
            }else{
                return DatabaseOperatePermission::Student;
            }
        }
    }else{
        return DatabaseOperatePermission::Student;
    }
    return DatabaseOperatePermission::Student;
}
